package com.hyd.tweet.twitter.command;

import com.hyd.tweet.twitter.StatusFormatter;
import twitter4j.Status;
import twitter4j.TwitterException;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 获取最近的关注推，最多 20 条
 *
 * @author yiding.he
 */
public class GetCommand extends Command {

    @Override
    public List<String> processCommand() throws TwitterException {

        List<String> result = new ArrayList<String>();
        List<Status> statuses = twitter.getHomeTimeline();  // 不带参数返回最多 20 条

        // 返回的顺序是倒排的，根据聊天习惯，改为顺排
        Collections.reverse(statuses);

        int new_message_count = 0;
        long since = user.getLastTimelineStatusId() == null ? 1 : user.getLastTimelineStatusId();

        long biggest = since;
        for (Status status : statuses) {

            if (status.getId() > since) {
                new_message_count++;
            }

            int rollerId = user.pushToRoller(status);
            result.add(StatusFormatter.formatStatus(twitterUser, status, rollerId));

            if (biggest < status.getId()) {
                biggest = status.getId();
            }
        }

        String suffix = user.getUnavailableTip();

        if (new_message_count == 0) {
            result.add("——已列出最近20条，没有新推" + suffix);
        } else {
            user.setLastTimelineStatusId(biggest);
            result.add("——已列出最近20条，" + new_message_count + "条新推" + suffix);
        }
        return result;
    }

}
